/*
This file is part of maven-bugzilla-plugin.

    maven-bugzilla-plugin is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    maven-bugzilla-plugin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with maven-bugzilla-plugin.  If not, see <http://www.gnu.org/licenses/>.
 */
package nl.semlab.maven.plugin.bugzilla;

import nl.semlab.maven.plugin.bugzilla.exceptions.BugzillaException;

/**
 * This mojo is executed when a release is being done. It is capable of adding
 * the release version of the project to Bugzilla as both a version and a
 * milestone.
 * <p />
 * Project maven-bugzilla-plugin<br />
 * BugzillaProductUpdateMojo.java created 09 Apr 2009
 * <p />
 * Copyright &copy 2009 SemLab
 * 
 * @author <a href="mailto:borsje@semlab.nl">J.A. Borsje</a>
 * @version $Revision: 1.1 $, $Date: 2009-08-03 10:04:21 $
 * @goal update-product
 * @phase deploy
 */
public class BugzillaProductUpdateMojo extends AbstractBugzillaMojo
{
   /**
    * True if a version for the release version should be added to Bugzilla,
    * false otherwise.
    * 
    * @parameter
    */
   private boolean addVersion;

   /**
    * True if a milestone for the release version should be added to Bugzilla,
    * false otherwise.
    * 
    * @parameter
    */
   private boolean addMilestone;

   /**
    * Create the Bugzilla version mojo.
    */
   public BugzillaProductUpdateMojo()
   {
      super();
   }

   /**
    * @see org.apache.maven.plugin.Mojo#execute()
    */
   // @edu.umd.cs.findbugs.annotations.SuppressWarnings(value =
   // "UWF_UNWRITTEN_FIELD", justification =
   // "The version and milestone are injected by Maven, they are not unwritten.")
   public void execute()
   {
      this.getLog().info("Updating product.");
      try
      {
         // Initialize the mojo.
         super.init();
         super.webservice.login();

         // Add the version.
         this.getLog().debug("Add version: " + addVersion);
         try
         {
            this.addVersion();
         }
         catch (BugzillaException ex)
         {
            this.getLog().warn("Could not add version [" + version + "], this has to be done by hand.", ex);
         }

         // Add the miletone.
         this.getLog().debug("Add milestone: " + addMilestone);
         try
         {
            this.addMilestone();
         }
         catch (BugzillaException ex)
         {
            this.getLog().warn("Could not add milestone [" + version + "], this has to be done by hand.", ex);
         }
      }
      catch (BugzillaException ex)
      {
         this.getLog().warn("Login to bugzilla failed.", ex);
      }
   }

   /**
    * Add a version with the current version number to Bugzilla.
    * 
    * @throws BugzillaException
    *            Thrown when the version could not be added.
    */
   private void addVersion() throws BugzillaException
   {
      if (addVersion)
      {
         webservice.addVersion(product, version);
      }
   }

   /**
    * Add a milestone with the current version number to Bugzilla. This does not
    * happen if we are building a snapshot.
    * 
    * @throws BugzillaException
    *            Thrown when the milestone could not be added.
    */
   private void addMilestone() throws BugzillaException
   {
      if (addMilestone && !version.endsWith("-SNAPSHOT"))
      {
         webservice.addMilestone(product, version);
      }
   }
}